//
// index.js
// vue3-element-admin
//
// Created by 区区电脑 on 2023/04/10.
//


import {createRouter, createWebHashHistory} from "vue-router";
import NProgress from "nprogress";
import {hasPage, hasToken, hasLogin, hasPagePermission, registerComponent} from "./config.js"
import LocalDB from "@/utils/LocalDB.js"
import store from "../store/index.js"
import {computed} from "vue"

const layoutType = computed(() => store.getters.layoutType)

const redirect = window.location.hash.replace("#", "");
if (redirect !== "/login") {
    LocalDB.setItem("redirect", redirect)
}

// 配置路由源数据
const routes = [
    {
        path: "/download",
        name: "download",
        component: () => import("@business/Download/Index.vue"),
    },
    {
        path: "/chinese-getter",
        name: "ChineseGetter",
        component: () => import("@business/BaseData/ProgramLanguage/ChineseGetter.vue"),
    },
    {
        path: "/print-template-designer",
        name: "PrintTemplateDesigner",
        component: () => import("@business/PrintTemplateManage/Designer/Index.vue"),
    },
    {
        path: "/print-template-preview",
        name: "PrintTemplatePreview",
        component: () => import("@business/PrintTemplateManage/Preview/Index.vue"),
    },
    {
        path: "/map-test-page",
        name: "mapTestPage",
        component: () => import("@business/MapTestPage.vue"),
    },
    {
        path: "/",
        name: "layout",
        component: () => layoutType.value === 'pc' ?  import("@/layouts/pc/index.vue") : import("@/layouts/mobile/index.vue") ,
    },
    {
        path: "/menu",
        name: "menu",
        component: () => import("@business/System/Menu/Index.vue"),
    },
    {
        path: "/login",
        component: () => import("@/userPages/login.vue"),
    },
    {
        path: "/initMenu",
        component: () => import("@/userPages/initMenu.vue"),
        meta: {
            mustLogin: true
        }
    },
    {
        path: "/local-print",
        component: () => import("@business/LocalPrint/Index.vue"),
        meta: {
            //mustLogin: true
        }
    },
    {
        path: "/nopermission",
        component: () => import("@/userPages/nopermission.vue"),
    },
    {
        path: "/notfound",
        component: () => import("@/userPages/notfound.vue"),
    },
    {
        path: "/:catchAll(.*)",
        redirect: '/notfound',
    }
];
// 创建路由
const router = createRouter({routes, history: createWebHashHistory()});

router.beforeEach(async (to, from, next) => {
    NProgress.start()
    NProgress.set(0.8)
    const path = to.path;
    // 目标地址是 / 并且是个空地址，默认跳转到工作台
    if (path === "/" && !to.query.page) {
        console.primary("空路由: ", to)
        return next("/?page=/workbenches&titleName=工作台")
    }
    // 目标地址不是 / 并且路由中已经配置了其他路由，直接跳转
    if (path !== "/" && routes.findIndex(item => item.path === path) > -1) {
        // 虽然已经配置了路由,但是也校验必须登录的页面
        if (to.meta.mustLogin) {
            const isLogin = await hasLogin()
            if (!isLogin) return next("/login");
        }
        // 虽然已经配置了路由,但是也校验必须有权限的页面
        if (to.meta.mustPermission) {
            await hasPagePermission(to, next)
        }
        return next();
    }

    // 如果不存在文件，跳转 404
    if (!hasPage(to)) return next("/notfound");
    if (!(await hasToken())) return next("/login");
    if (!(await hasPagePermission(to, next))) return next("/nopermission");
    await registerComponent(to);
    next()
});

// 路由后置守卫
router.afterEach((to) => {
    if(layoutType.value === "mobile" && to.query.titleName) {
        document.title = to.query.titleName;
    }
    NProgress.done();
});
export default router;
